package com.yyge.common.aspect;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yyge.common.util.WaimaiThreadLocal;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    /**
     * 调用mybatisplus  save()方法
     * @param metaObject  真实的对象（实体对象）
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        if(WaimaiThreadLocal.getCurrent()!=null){
         this.strictInsertFill(metaObject, "createBy", String.class, WaimaiThreadLocal.getName());
        }
        System.out.println("你要调用mobileLoginThreadLocal");
        System.out.println(WaimaiThreadLocal.mobileLoginThreadLocal);
        if(WaimaiThreadLocal.mobileLoginThreadLocal!=null){
         this.strictInsertFill(metaObject, "createBy", String.class, WaimaiThreadLocal.mobileLoginThreadLocal.get().getUserName());
        }
        //this.strictInsertFill(metaObject, "createBy", String.class, WaimaiThreadLocal.getName());
    }


    /**
     * 调用mybatisplus  updateById()方法
     * @param metaObject 真实的对象（实体对象）
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        if(WaimaiThreadLocal.getCurrent()!=null) {
            this.strictInsertFill(metaObject, "updateBy", String.class, WaimaiThreadLocal.getName());
        }
        if(WaimaiThreadLocal.mobileLoginThreadLocal!=null){
         this.strictInsertFill(metaObject, "updateBy", String.class, WaimaiThreadLocal.mobileLoginThreadLocal.get().getUserName());
        }
    }
}
